//
//  MSTradeViewController.m
//  SocietySale
//
//  Created by Mike on 16/10/25.
//  Copyright © 2016年 Mike. All rights reserved.
//

#define BtnWidth           (SCREEN_WIDTH/2)

#import "MSTradeViewController.h"
#import "MSTradeTableViewController.h"
#import "MSSearchViewController.h"
#import "MSMytradeViewController.h"
#import "MSDoubleListFilterVC.h"
#import "MSSelectTimeView.h"
#import "MSPreferenceSetViewController.h"
#import "MSProjectDetailViewController.h"
#import "MSCustomerRecommenVC.h"
#import "MSCustomerMatchingVC.h"
#import "MSMemberServeViewController.h"

@interface MSTradeViewController ()
{
    BOOL isSelectArea;
    BOOL isSelectIndustry;
    BOOL isSelectStage;
    NSString *documentsPath;
    NSFileManager* fileManager;
    NSString *currentProviceStr;
    NSString *currentCityStr;
    NSString *currentTypeStr;
    NSString *currentStateStr;
    NSString *currentIndustryStr;
    NSString *currentTimeStr;
}

@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, assign) float  positionY;
//侧边view
@property (nonatomic, strong) UIImageView *sideView;
@property (nonatomic, strong) GXButton            *tradeBtn;
@property (nonatomic, strong) GXButton            *releaseBtn;
@property (nonatomic, strong) GXButton            *searchBtn;
@property (nonatomic, strong) HDImageMenuView     *menu;
@property (nonatomic, strong) MSSaleFilterViewController        *typeFilterVC;       //类型筛选
@property (nonatomic, strong) MSSaleFilterViewController        *stateFilterVC;      //状态筛选
@property (nonatomic, strong) MSDoubleListFilterVC              *areaFilterVC;       //地区筛选
@property (nonatomic, strong) MSSaleFilterViewController        *industryFilterVC;   //行业筛选
@property (nonatomic, strong) MSSaleFilterViewController        *timeFilterVC;       //时间筛选
@property (nonatomic, strong) MSSaleFilterViewController        *filterVC;           //筛选控件
@property (nonatomic, strong) MSSelectTimeView                  *selectTimeView;     //选择时间
@property (nonatomic, strong) MSTradeTableViewController        *tableVC;
@property (nonatomic, strong) NSMutableArray                    *filterVCArray;
@property (nonatomic, strong) NSMutableArray                    *stateVCArray;
@property (nonatomic, assign) NSInteger lastIndex;
@property (nonatomic, assign) BOOL selectTime;
@property (nonatomic, copy) NSString *areaStr;
@property (nonatomic, copy) NSString *industryStr;
@property (nonatomic, copy) NSString *stateStr;
@end

@implementation MSTradeViewController

- (void)initNavigationBar{
    [super initNavigationBar];
    titleLable.text = @"交易";
    rightBarBtn.hidden = NO;
    [rightBarBtn setImage:[UIImage imageNamed:@"plus"] forState:UIControlStateNormal];
    [rightBarBtn addTarget:self action:@selector(rightBarBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    _searchBtn = [GXButton buttonWithType:UIButtonTypeCustom];
    _searchBtn.relayout = YES;
    _searchBtn.imageViewFrame = CGRectMake(10, 12, 21, 20);
    _searchBtn.tag = 30;
    _searchBtn.frame = CGRectMake(SCREENWIDTH-90, 20, 40, 44);
    [_searchBtn setImage:[UIImage imageNamed:@"search_white"] forState:UIControlStateNormal];
    [_searchBtn addTarget:self action:@selector(headBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_navBarView addSubview:_searchBtn];
}

- (void)initUI{
    NSArray *titleArr = [NSArray arrayWithObjects:@"类型",@"状态",@"地区",@"行业",@"时间", nil];
    self.menu = [[HDImageMenuView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40) titleArray:titleArr];
    self.menu.delegate = (id)self;
    self.menu.isHiddenBottomLine = NO;
    [self.contentView addSubview:self.menu];
    
    self.tableVC = [[MSTradeTableViewController alloc] initWithStyle:UITableViewStylePlain];
    self.tableVC.view.frame = CGRectMake(0, 40, SCREEN_WIDTH, SCREEN_HEIGHT-KNavBarHeight-KTabBarHeight-40);
    self.tableVC.enableRefresh = YES;
    self.tableVC.enableRequest = YES;
    self.tableVC.delegate = (id)self;
    self.tableVC.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.contentView addSubview:self.tableVC.view];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
    tapGesture.delegate = (id)self;
    [self.view addGestureRecognizer:tapGesture];
    
    [self layoutHeadView];

    _positionY = 40;
    _lastIndex = -1;
    self.industryFilterVC = [self creatFilterVC];
    self.stateFilterVC = [self creatFilterVC];
    self.typeFilterVC = [self creatFilterVC];
    self.timeFilterVC = [self creatFilterVC];
    
    self.filterVCArray = [NSMutableArray arrayWithObjects:self.typeFilterVC,self.stateFilterVC,self.areaFilterVC,self.industryFilterVC,self.timeFilterVC, nil];
    
//    self.typeFilterVC.dataArray = [@[@"全部",@"出售",@"求购"] mutableCopy];
//    self.stateFilterVC.dataArray = [@[@"全部",@"正在交易", @"公开显示", @"过期交易"] mutableCopy];
//    self.industryFilterVC.dataArray = [@[@"推荐",@"电力", @"能源矿产", @"冶金", @"化工医疗", @"轻纺食品", @"环保", @"市政公用", @"交通运输", @"机械电子", @"建筑", @"建材", @"农林水利", @"生态休闲旅游", @"其他"] mutableCopy];
//    self.timeFilterVC.dataArray = [@[@"不限",@"最近1个月",@"最近3个月",@"最近半年",@"最近1年",@"自定义"] mutableCopy];

    _selectTimeView = [[MSSelectTimeView alloc] initWithFrame:CGRectMake(0, 224, SCREEN_WIDTH, SCREEN_HEIGHT-224)];
    _selectTimeView.hidden = YES;
    _selectTimeView.delegate = (id)self;
    UIWindow *window = [UIApplication sharedApplication].windows[0];
    [window addSubview:_selectTimeView];
    
    currentProviceStr = @"";
    currentCityStr = @"";
    currentIndustryStr = @"";
    currentStateStr = @"";
    currentTypeStr = @"";
    currentTimeStr = @"";
    
    [self initPreference];
}

- (void)dismiss{
    _sideView.hidden = YES;
    rightBarBtn.selected = NO;
}

- (void)rightBarBtnAction:(GXButton*)btn{
    btn.selected = !btn.selected;
    if (btn.selected) {
        if (!_sideView) {
            _sideView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-85, 70, 80, 210)];
            UIImage *newImage = [[UIImage imageNamed:@"pop_bg"] stretchableImageWithLeftCapWidth:0 topCapHeight:35];
            _sideView.image = newImage;
            _sideView.userInteractionEnabled = YES;
            _sideView.clipsToBounds = YES;
            [self.view addSubview:_sideView];
            NSArray *titleArray = @[@"发布交易",@"客户追踪",@"客户引荐",@"会员服务",@"偏好设置"];
            for (int i =0; i<5; i++) {
                GXButton *sideButton = [GXButton buttonWithType:UIButtonTypeCustom];
                [sideButton setFrame:CGRectMake(0, 5+i*40, 80, 40)];
                sideButton.tag = 10+i;
                //                [sideButton setImage:[UIImage imageNamed:titleArray[i]] forState:UIControlStateNormal];
                sideButton.titleLabel.font = HeitiFont(15);
                [sideButton setTitle:titleArray[i] forState:UIControlStateNormal];
                [sideButton setTitleColor:kLightGrayColor forState:UIControlStateHighlighted];
                [sideButton addTarget:self action:@selector(sideButtonAction:) forControlEvents:UIControlEventTouchUpInside];
                [_sideView addSubview:sideButton];
                
                //                UIView *sublineView = [[UIView alloc] initWithFrame:CGRectMake(10, sideButton.bottom, 60, 0.5)];
                //                sublineView.backgroundColor = kLightGrayColor;
                //                [_sideView addSubview:sublineView];
                //                if (i == 4) {
                //                    sublineView.hidden = YES;
                //                }
            }
            
        }
        _sideView.hidden = NO;
    }
    else{
        _sideView.hidden = YES;
    }
    if (_lastIndex != -1) {
        self.filterVC = self.filterVCArray[_lastIndex];
        if ([self.filterVC.view superview]) {
            [self.filterVC dismiss];
        }
        [self.menu setBtnStateWithIndex:_lastIndex state:NO];
        [self.menu setBtnTitleColorWithIndex:_lastIndex state:NO];
    }
    if (!_selectTimeView.hidden) {
        _selectTimeView.hidden = YES;
    }
    if ([self.areaFilterVC.view superview]) {
        [self.areaFilterVC dismiss];
    }
}

- (void)sideButtonAction:(GXButton*)btn{
    if (btn.tag == 10) {
        MSReleaseTradeViewController *vc = [[MSReleaseTradeViewController alloc] init];
        [rootNavigation pushViewController:vc animated:YES];
    }
    else if (btn.tag == 11) {
        MSCustomerMatchingVC *vc = [[MSCustomerMatchingVC alloc] init];
        [rootNavigation pushViewController:vc animated:YES];
    }
    else if (btn.tag == 12) {
        MSCustomerRecommenVC *vc = [[MSCustomerRecommenVC alloc] init];
        [rootNavigation pushViewController:vc animated:YES];
    }
    else if (btn.tag == 13) {
        MSMemberServeViewController *vc = [[MSMemberServeViewController alloc] init];
        vc.memberType = MemberType_advanced;
        [rootNavigation pushViewController:vc animated:YES];
    }
    else if (btn.tag == 14) {
        MSPreferenceSetViewController *vc = [[MSPreferenceSetViewController alloc] init];
        [rootNavigation pushViewController:vc animated:YES];
    }
    _sideView.hidden = YES;
    rightBarBtn.selected = NO;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [netEngine requestMinePageWithTarget:self.VctrID hudView:nil];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    if ([self.filterVC.view superview]) {
        [self.filterVC dismiss];
    }
    if (!_selectTimeView.hidden) {//存在选择时间控件
        _selectTimeView.hidden = YES;
    }
    if ([self.areaFilterVC.view superview]) {
        [self.areaFilterVC dismiss];
    }
//    _sideView.hidden = YES;
    rightBarBtn.selected = NO;
    [self.menu setBtnStateWithIndex:_lastIndex state:NO];
}

- (NSMutableArray *)creatStateArrayWithArray:(NSArray*)array{
    NSMutableArray *mutalArray = [[NSMutableArray alloc] initWithCapacity:0];
    for (int i= 0; i<array.count; i++) {
        [mutalArray addObject:@"NO"];
    }
    return mutalArray;
}

- (void)layoutHeadView{
    _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100)];
    _headerView.backgroundColor = [UIColor whiteColor];
    self.tableVC.tableView.tableHeaderView = _headerView;
    
    NSMutableAttributedString *tradAttstr = [[NSMutableAttributedString alloc] initWithString:@"我的交易\n" attributes:@{NSForegroundColorAttributeName:kDarkGrayColor}];
    NSAttributedString *tradStr = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"0"]  attributes:@{NSForegroundColorAttributeName:kLightBlackColor}];
    [tradAttstr appendAttributedString:tradStr];
    
    self.tradeBtn = [self creatBtnWithFrame:CGRectMake(0, 0, BtnWidth, 50) andImage:@"" andTitle:@""];//jiaoyi1
//    [self.tradeBtn setAttributedTitle:tradAttstr forState:UIControlStateNormal];
    [self.tradeBtn setTitle:[NSString stringWithFormat:@"我的交易 %@  》",@"0"] forState:UIControlStateNormal];
    
    self.releaseBtn = [self creatBtnWithFrame:CGRectMake(BtnWidth, 0, SCREEN_WIDTH/2, 50) andImage:@"" andTitle:@"发布交易  》"];//fabujiaoyi
    [self.releaseBtn setTitleColor:kLightBlackColor forState:UIControlStateNormal];
    self.tradeBtn.tag = 10;
    self.releaseBtn.tag = 20;

    UIView *horizonLineView =[[UIView alloc] initWithFrame:CGRectMake(BtnWidth, 0, 0.6, 50)];
    horizonLineView.backgroundColor = kNormalGray;
    [_headerView addSubview:horizonLineView];
    
    UIView *straightLineView =[[UIView alloc] initWithFrame:CGRectMake(0, _tradeBtn.bottom-0.6, SCREEN_WIDTH, 0.6)];
    straightLineView.backgroundColor = kNormalGray;
    [_headerView addSubview:straightLineView];

    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, _tradeBtn.bottom, SCREENFRAME.size.width, 50)];
    bgView.backgroundColor = kDefaultViewBackgroundColor;
    [_headerView addSubview:bgView];
    
//    self.searchBtn = [self creatBtnWithFrame:CGRectMake(12, 80, SCREEN_WIDTH-24, 30) andImage:@"search" andTitle:@" 搜索交易信息"];
//    GXButton *setBtn1 = [self creatBtnWithFrame:CGRectMake(12, 80, SCREEN_WIDTH-52, 30) andImage:@"" andTitle:@"我的订阅"];
//    setBtn1.titleLabelFrame = CGRectMake(10, 0, 100, 30);
//    setBtn1.layer.borderColor = kNormalGray.CGColor;
//    setBtn1.layer.borderWidth = 0.6;
//    setBtn1.layer.cornerRadius = 2;
//    setBtn1.backgroundColor = [UIColor whiteColor];
//    setBtn1.titleLabel.textAlignment = NSTextAlignmentLeft;
//    setBtn1.tag = 50;
//    self.searchBtn.tag = 30;
}

- (void)setHeadTitleWithUserModel:(MSUserInfoModel*)userModel{
    NSMutableAttributedString *tradAttstr = [[NSMutableAttributedString alloc] initWithString:@"我的交易\n" attributes:@{NSForegroundColorAttributeName:kDarkGrayColor}];
    NSAttributedString *tradStr = [[NSAttributedString alloc] initWithString:userModel.tradeNum attributes:@{NSForegroundColorAttributeName:kLightBlackColor}];
    [tradAttstr appendAttributedString:tradStr];
//    [self.tradeBtn setAttributedTitle:tradAttstr forState:UIControlStateNormal];
    [self.tradeBtn setTitle:[NSString stringWithFormat:@"我的交易 %@  》",userModel.tradeNum] forState:UIControlStateNormal];
}

//创建btn
- (GXButton*)creatBtnWithFrame:(CGRect)frame andImage:(NSString*)imageStr  andTitle:(NSString*)title{
    GXButton *btn = [GXButton buttonWithType:UIButtonTypeCustom];
//    btn.relayout = YES;
    btn.frame = frame;
//    btn.imageViewFrame = CGRectMake((BtnWidth-90)/2, 15, 40, 40);
//    btn.titleLabelFrame = CGRectMake((BtnWidth-90)/2+40, 0, 60, 70);
    //    [btn.titleLabel setBackgroundColor:kDarkGrayColor];
    btn.titleLabel.textAlignment = NSTextAlignmentCenter;
//    btn.titleLabel.numberOfLines = 0;
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:kLightBlackColor forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont fontWithName:@"STHeitiSC-Light" size:13];
    [btn setImage:[UIImage imageNamed:imageStr] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(headBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_headerView addSubview:btn];
    return btn;
}

- (NSMutableAttributedString *)setTitleWithTitle:(NSString*)title andValue:(NSString*)value{
    NSMutableAttributedString *attriStr = [[NSMutableAttributedString alloc] initWithString:title];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 1;
    [attriStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0,title.length+value.length)];

    NSAttributedString *titleStr = [[NSAttributedString alloc] initWithString:@"" attributes:@{NSFontAttributeName:HeitiFont(13),NSForegroundColorAttributeName:kDarkGrayColor}];
    [attriStr appendAttributedString:titleStr];

    return attriStr;
}

- (void)initPreference{
    //区域
    NSString *areaCodeFile = [documentsPath stringByAppendingPathComponent:@"areaCodeFile.plist"];
    if ([fileManager fileExistsAtPath:areaCodeFile]) {
        NSMutableArray *areaCodeArray = [[NSMutableArray alloc] initWithContentsOfFile:areaCodeFile];
        _areaStr = [areaCodeArray componentsJoinedByString:@","];
        currentProviceStr = _areaStr;
        //        isSelectArea = NO;
    }
    else{
        _areaStr = @"";
    }
    
    //行业
    NSString *industryCodeFile = [documentsPath stringByAppendingPathComponent:@"industryCodeFile.plist"];
    if ([fileManager fileExistsAtPath:industryCodeFile]) {
        NSMutableArray *industryCodeArray = [[NSMutableArray alloc] initWithContentsOfFile:industryCodeFile];
        _industryStr = [industryCodeArray componentsJoinedByString:@","];
        currentIndustryStr = _industryStr;
        //        isSelectIndustry = NO;
    }
    else{
        _industryStr = @"";
    }
    
    self.tableVC.pageNum = 1;
    [self request:NO];
}

- (void)testDataFunc{
    fileManager = [NSFileManager defaultManager];
    documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) firstObject];
    
    //类型
    NSString *tradeTypeFile = [documentsPath stringByAppendingPathComponent:@"tradeTypeFile.plist"];
    if ([fileManager fileExistsAtPath:tradeTypeFile]) {
        netEngine.tradeTypeArray = [[NSMutableArray alloc] initWithContentsOfFile:tradeTypeFile];
    }
    if (netEngine.tradeTypeArray.count<1) {
        [netEngine requestTradeScreeningConditionsWithTarget:self.VctrID hudView:nil];
    }
    [self fillFilterData];
    
    MSTradeModel *model = [[MSTradeModel alloc] init];
    model.title = @"上海浦东世和空港中心商业住房项目";
    model.projectType = @"求购业主联系人信息";
    model.tradeState = @"交易中";
    model.tradeNum = @"3次交易";
    model.name = @"周世鹏";
    model.position = @"经理";
    model.theme = @"电缆附件招标最新进展";
    model.content = @"杭州地铁二号线目前极影施工阶段";
    model.tradeEvaluate = @"评价              ";
    model.tradePrice = @"12";
    model.memberType = @"[匿名会员]";
    model.memberLevel = @"2钻会员";
    model.time = @"2016.9.15";
    
    MSTradeModel *model1 = [[MSTradeModel alloc] init];
    model1.title = @"上海浦东世和空港中心商业住房项目";
    model1.projectType = @"出售进展信息";
    model1.tradeState = @"已过期";
    model1.tradeNum = @"5次交易";
    model1.name = @"王鹏辉";
    model1.position = @"主管";
    //    model1.theme = @"电缆附件招标最新进展";
    //    model1.content = @"杭州地铁二号线目前极影施工阶段";
    //    model1.tradeEvaluate = @"评价              ";
    model1.tradePrice = @"23";
    model1.memberType = @"[系统录入]";
    model1.memberLevel = @"3";
    model1.time = @"2016.9.15";
    
    MSTradeModel *model2 = [[MSTradeModel alloc] init];
    model2.title = @"上海浦东世和空港中心商业住房项目";
    model2.projectType = @"求购设计师项目联系人信息";
    model2.tradeState = @"公开显示";
    model2.tradeNum = @"23次交易";
    //    model2.name = @"李光成";
    model2.position = @"总监";
    model2.theme = @"电缆附件招标最新进展";
    model2.content = @"杭州地铁二号线目前极影施工阶段";
    model2.tradeEvaluate = @"评价              ";
    model2.tradePrice = @"45";
    model2.memberType = @"[匿名会员]";
    model2.memberLevel = @"1";
    model2.time = @"2016.9.15";
    
//    self.tableVC.dataArray = [@[model,model1,model2,model1,model,model1,model2] mutableCopy];
    //    [self request:NO];
}

- (void)fillFilterData{
    //省市
    NSMutableArray *tempDataArray = [[NSMutableArray alloc] initWithCapacity:0];
    NSMutableArray *tempCodeArray = [[NSMutableArray alloc] initWithCapacity:0];
    NSString *provincesFile = [documentsPath stringByAppendingPathComponent:@"provincesFile.plist"];
    if ([fileManager fileExistsAtPath:provincesFile]) {
        netEngine.provincesArray = [[NSMutableArray alloc] initWithContentsOfFile:provincesFile];
    }
    for (NSDictionary *dict in netEngine.provincesArray) {
        if ([dict isKindOfClass:[NSDictionary class]]) {
            NSArray *tempArray = [HDUtil checkArray:dict[@"citys"]];
            NSMutableArray *citysArray = [NSMutableArray arrayWithCapacity:0];
            NSMutableArray *citysCodeArray = [NSMutableArray arrayWithCapacity:0];
            for (NSDictionary *provinceDict in tempArray) {
                if ([provinceDict isKindOfClass:[NSDictionary class]]){
                    [citysArray addObject:[HDUtil check:provinceDict[@"name"]]];
                    [citysCodeArray addObject:[HDUtil check:provinceDict[@"code"]]];
                }
            }
            NSDictionary *chinaDict = @{[HDUtil check:dict[@"name"]]:citysArray};
            NSDictionary *chinaCodeDict = @{[HDUtil check:dict[@"code"]]:citysCodeArray};
            [tempDataArray addObject:chinaDict];
            [tempCodeArray addObject:chinaCodeDict];
        }
    }
    self.areaFilterVC.codeArray = tempCodeArray;
    self.areaFilterVC.dataArray = tempDataArray;
    
    //类型
    NSMutableArray *tempDataArray4 = [[NSMutableArray alloc] initWithCapacity:0];
    NSMutableArray *tempCodeArray4 = [[NSMutableArray alloc] initWithCapacity:0];
    NSString *projectInvestmentFile = [documentsPath stringByAppendingPathComponent:@"tradeTypeFile.plist"];
    if ([fileManager fileExistsAtPath:projectInvestmentFile]) {
        netEngine.tradeTypeArray = [[NSMutableArray alloc] initWithContentsOfFile:projectInvestmentFile];
    }
    for (NSDictionary *dict in netEngine.tradeTypeArray) {
        if ([dict isKindOfClass:[NSDictionary class]]) {
            [tempDataArray4 addObject:[HDUtil check:dict[@"name"]]];
            [tempCodeArray4 addObject:[HDUtil check:dict[@"code"]]];
        }
    }
    self.typeFilterVC.dataArray = tempDataArray4;
    self.typeFilterVC.codeArray = tempCodeArray4;

    //状态
    NSMutableArray *tempDataArray3 = [[NSMutableArray alloc] initWithCapacity:0];
    NSMutableArray *tempCodeArray3 = [[NSMutableArray alloc] initWithCapacity:0];
    NSString *projectStateFile = [documentsPath stringByAppendingPathComponent:@"tradeStateFile.plist"];
    if ([fileManager fileExistsAtPath:projectStateFile]) {
        netEngine.tradeStateArray = [[NSMutableArray alloc] initWithContentsOfFile:projectStateFile];
    }
    for (NSDictionary *dict in netEngine.tradeStateArray) {
        if ([dict isKindOfClass:[NSDictionary class]]) {
            [tempDataArray3 addObject:[HDUtil check:dict[@"name"]]];
            [tempCodeArray3 addObject:[HDUtil check:dict[@"code"]]];
        }
    }
    self.stateFilterVC.dataArray = tempDataArray3;
    self.stateFilterVC.codeArray = tempCodeArray3;
    
    //行业
    NSMutableArray *tempDataArray2 = [[NSMutableArray alloc] initWithCapacity:0];
    NSMutableArray *tempCodeArray2 = [[NSMutableArray alloc] initWithCapacity:0];
    
    NSString *projectIndustryFile = [documentsPath stringByAppendingPathComponent:@"projectIndustryFile.plist"];
    if ([fileManager fileExistsAtPath:projectIndustryFile]) {
        netEngine.projectIndustryArray = [[NSMutableArray alloc] initWithContentsOfFile:projectIndustryFile];
    }
    for (NSDictionary *dict in netEngine.projectIndustryArray) {
        if ([dict isKindOfClass:[NSDictionary class]]) {
            [tempDataArray2 addObject:[HDUtil check:dict[@"name"]]];
            [tempCodeArray2 addObject:[HDUtil check:dict[@"code"]]];
        }
    }
    self.industryFilterVC.dataArray = tempDataArray2;
    self.industryFilterVC.codeArray = tempCodeArray2;

    //时间
    NSMutableArray *tempDataArray5 = [[NSMutableArray alloc] initWithCapacity:0];
    NSMutableArray *tempCodeArray5 = [[NSMutableArray alloc] initWithCapacity:0];
    NSString *projectTimeFile = [documentsPath stringByAppendingPathComponent:@"projectTimeFile.plist"];
    if ([fileManager fileExistsAtPath:projectTimeFile]) {
        netEngine.projectTimeArray = [[NSMutableArray alloc] initWithContentsOfFile:projectTimeFile];
    }
    for (NSDictionary *dict in netEngine.projectTimeArray) {
        if ([dict isKindOfClass:[NSDictionary class]]) {
            [tempDataArray5 addObject:[HDUtil check:dict[@"name"]]];
            [tempCodeArray5 addObject:[HDUtil check:dict[@"code"]]];
        }
    }
    self.timeFilterVC.dataArray = tempDataArray5;
    self.timeFilterVC.codeArray = tempCodeArray5;
    
    self.typeFilterVC.stateArray = [self creatStateArrayWithArray:self.typeFilterVC.dataArray];
    self.stateFilterVC.stateArray = [self creatStateArrayWithArray:self.stateFilterVC.dataArray];
    self.industryFilterVC.stateArray = [self creatStateArrayWithArray:self.industryFilterVC.dataArray];
    self.timeFilterVC.stateArray = [self creatStateArrayWithArray:self.timeFilterVC.dataArray];
    
    self.stateVCArray = [NSMutableArray arrayWithObjects:self.typeFilterVC.stateArray,self.stateFilterVC.stateArray,self.areaFilterVC,self.industryFilterVC.stateArray,self.timeFilterVC.stateArray, nil];
}

- (void)updateViewController:(NSNotification *)note {
    NSDictionary *responseDic = note.userInfo;
    if (responseDic[Receiver]==self.VctrID) {
        if (responseDic[@"MinePage"]!=nil){
            MSUserInfoModel *userModel = responseDic[@"MinePage"];
            [self setHeadTitleWithUserModel:userModel];
        }
        if (responseDic[@"TradeList"]!=nil){
            if (responseDic[@"state"]!=nil){
                self.tableVC.totalPagesNum = [responseDic[@"totalPage"] intValue];
                if (self.tableVC.pageNum == 1) {
                    self.tableVC.dataArray = nil;
                }
                NSMutableArray *dataArrray = [[NSMutableArray alloc] initWithArray:self.tableVC.dataArray];
                [dataArrray addObjectsFromArray:responseDic[@"TradeList"]];
                self.tableVC.dataArray = dataArrray;
                
            }
            else{
                self.tableVC.dataArray = self.tableVC.dataArray;
            }
        }
        if (responseDic[@"TradeScreeningConditions"]!=nil){
            [self fillFilterData];
        }
    }
}

#pragma mark DYTableViewProtocol
/** 上拉 */
- (void)pullTableView:(UITableView *)tableView isNextPage:(BOOL)isNextPage{
    self.tableVC.pageNum++;
    [self request:YES];
}

- (void)pullRefreshRequest
{
    self.tableVC.pageNum = 1;
    [self request:YES];
}

-(void)request:(BOOL)isPull
{
    UIView * hudView = isPull?nil:self.contentView;
    NSString *pageNumber = [NSString stringWithFormat:@"%d",self.tableVC.pageNum];
    
    NSDictionary *dict = @{@"province":currentProviceStr,
                           @"city":currentCityStr,
                           @"industry":currentIndustryStr,
                           @"stages":currentStateStr,
                           @"type":currentTypeStr,
                           @"onlineTime":currentTimeStr,
                           @"pageNo":pageNumber,
                           @"pageSize":@"10"};
    [netEngine requestTradeListWithTarget:self.VctrID hudView:hudView params:dict];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MSTradeModel *model = self.tableVC.dataArray[indexPath.row];
    if (model.isSelf) {
        [HDUtil showMessage:@"自己不能交易自己发布的交易"];
        return;
    }
    MyTradeConfirmViewController *vc = [[MyTradeConfirmViewController alloc] init];
    vc.model = model;
    [rootNavigation pushViewController:vc animated:YES];
}

- (void)headBtnClick:(GXButton *)btn{
    if (btn.tag == 10) {
        MSMytradeViewController *vc = [[MSMytradeViewController alloc] init];
        [rootNavigation pushViewController:vc animated:YES];
    }
    if (btn.tag == 20) {
        __weak MSTradeViewController *weakSelf = self;
        MSReleaseTradeViewController *vc = [[MSReleaseTradeViewController alloc] init];
        vc.choickBlock = ^(){
            weakSelf.tableVC.pageNum = 1;
            [weakSelf request:NO];
        };
        [rootNavigation pushViewController:vc animated:YES];
    }
    if (btn.tag == 30) {
        MSSearchViewController *vc = [[MSSearchViewController alloc] init];
        vc.searchType = MSSearchType_Trade;
        [rootNavigation pushViewController:vc animated:NO];
    }
}

- (void)selectedDate:(NSDate*)startDate end:(NSDate*)endDate{
    _selectTimeView.hidden = YES;
    _selectTime = YES;
    [self.menu setBtnStateWithIndex:_lastIndex state:NO];
    [self.menu setBtnTitleColorWithIndex:4 state:YES];
    [self.filterVC selectedTime];
}

#pragma mark - MSDoubleListFilterVCDelegate
- (void)selectedAreaProviceCode:(NSDictionary *)proviceDict cityCode:(NSDictionary *)cityDict isTap:(BOOL)isTap{//地区选择
    [self.menu setBtnStateWithIndex:_lastIndex state:NO];
    if (isTap) {
//        [self.menu setBtnStateWithIndex:2 state:NO];
        return;
    }
    NSString *cityName = [cityDict allKeys][0];
    if (cityName.length) {
        isSelectArea = YES;
        currentProviceStr = [proviceDict allValues][0];
        currentCityStr = [cityDict allValues][0];
        [self.menu setBtnTitleColorWithIndex:2 state:YES];
    }
    else{
        isSelectArea = NO;
        currentProviceStr = _areaStr;
        currentCityStr = @"";
        [self.menu setBtnTitleColorWithIndex:2 state:NO];
    }
    self.tableVC.pageNum = 1;
    [self request:NO];
}

#pragma mark - MSSaleFilterDelegate
- (void)selectedFilterWithParmas:(NSInteger)index code:(NSString*)codeValue{//筛选
    if (_lastIndex == 4) {//时间
        if (index == 100 && !_selectTime) {//自定义时间
            _selectTimeView.hidden = NO;
            _selectTimeView.frame = CGRectMake(0, _positionY+KNavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-_positionY - KNavBarHeight);
            [_selectTimeView showInView:SCREEN_HEIGHT-_positionY - KNavBarHeight];
            UIWindow *window = [UIApplication sharedApplication].windows[0];
            [window bringSubviewToFront:_selectTimeView];
            return;
        }
        else{
            [self.menu setBtnTitleColorWithIndex:4 state:YES];
        }
    }
    _selectTime = NO;
    [self.menu setBtnStateWithIndex:_lastIndex state:NO];
    NSArray *stateArray = self.stateVCArray[_lastIndex];
    if ([stateArray containsObject:@"YES"]) {
        [self.menu setBtnTitleColorWithIndex:_lastIndex state:YES];
    }
    else{
        [self.menu setBtnTitleColorWithIndex:_lastIndex state:NO];
    }

    if (_lastIndex == 0) {//类型
        if ([stateArray containsObject:@"YES"]) {
            currentTypeStr = codeValue;
        }
        else{
            currentTypeStr = @"";
        }
    }
    if (_lastIndex == 1) {//状态
        if ([stateArray containsObject:@"YES"]) {
            isSelectStage = YES;
            currentStateStr = codeValue;
        }
        else{
            isSelectStage = NO;
            currentStateStr = @"";
        }
    }
    if (_lastIndex == 3) {//行业
        if ([stateArray containsObject:@"YES"]) {
            isSelectIndustry = YES;
            currentIndustryStr = codeValue;
        }
        else{
            isSelectIndustry = NO;
            currentIndustryStr = _industryStr;
        }
    }
    if (_lastIndex == 4) {//时间
        if ([stateArray containsObject:@"YES"]) {
            currentTimeStr = codeValue;
        }
        else{
            currentTimeStr = @"";
        }
    }
    if (index != 1000) {
        self.tableVC.pageNum = 1;
        [self request:NO];
    }
}

- (void)selectImageMenuIndex:(int)index{
    //    if (!_sideView.hidden) {
    //        _sideView.hidden = YES;
    //        rightBarBtn.selected = NO;
    //    }
    if (!_selectTimeView.hidden) {//存在选择时间控件
        _selectTimeView.hidden = YES;
    }
    if (_lastIndex != -1) {//不是第一次
        [self.menu setBtnStateWithIndex:_lastIndex state:NO];
        if (index != 2) {//点击其它
            if (index != _lastIndex) {
                self.filterVC = self.filterVCArray[_lastIndex];
                if ([self.filterVC.view superview]) {
                    [self.filterVC dismiss];
                }
                self.filterVC = self.filterVCArray[index];
                if ([self.filterVC.view superview]) {
                    [self.filterVC dismiss];
                }
                else{
                    [self.filterVC showInView:_positionY];
                    [self.menu setBtnStateWithIndex:index state:YES];
                }
            }
            else{
                self.filterVC = self.filterVCArray[index];
                if ([self.filterVC.view superview]) {
                    [self.filterVC dismiss];
                }
                else{
                    [self.filterVC showInView:_positionY];
                    [self.menu setBtnStateWithIndex:index state:YES];
                }
            }
        }
        else{//点击地区
            if (_lastIndex != 2) {
                self.filterVC = self.filterVCArray[_lastIndex];
                if ([self.filterVC.view superview]) {
                    [self.filterVC dismiss];
                }
            }
            
            if ([self.areaFilterVC.view superview]) {
                [self.areaFilterVC dismiss];
                [self.menu setBtnStateWithIndex:index state:NO];
            } else {
                [self.areaFilterVC showInView:_positionY];
                [self.menu setBtnStateWithIndex:index state:YES];
            }
        }
    }
    else{//第一次点击
        if (index != 2) {// 其他
            self.filterVC = self.filterVCArray[index];
            if ([self.filterVC.view superview]) {
                [self.filterVC dismiss];
            }
            else {
                [self.filterVC showInView:_positionY];
                [self.menu setBtnStateWithIndex:index state:YES];
            }
        }
        else{//点击地区
            if ([self.areaFilterVC.view superview]) {
                [self.areaFilterVC dismiss];
                [self.menu setBtnStateWithIndex:index state:NO];
            } else {
                [self.areaFilterVC showInView:_positionY];
                [self.menu setBtnStateWithIndex:index state:YES];
            }
        }
    }
    _lastIndex = index;
}

- (MSSaleFilterViewController *)creatFilterVC
{
    MSSaleFilterViewController *VC = [[MSSaleFilterViewController alloc] init];
    VC.delegate = (id<MSSaleFilterDelegate>)self;
    return VC;
}

- (MSDoubleListFilterVC *)areaFilterVC
{
    if (!_areaFilterVC) {
        _areaFilterVC = [[MSDoubleListFilterVC alloc] init];
        _areaFilterVC.delegate=(id<MSDoubleListFilterVCDelegate>)self;
    }
    return _areaFilterVC;
}

#pragma mark - scrollviewBeginDragging
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    if ([self.filterVC.view superview]) {
        [self.filterVC dismiss];
    }
    if (!_selectTimeView.hidden) {//存在选择时间控件
        _selectTimeView.hidden = YES;
    }
    if ([self.areaFilterVC.view superview]) {
        [self.areaFilterVC dismiss];
    }
//    _sideView.hidden = YES;
    rightBarBtn.selected = NO;
    [self.menu setBtnStateWithIndex:_lastIndex state:NO];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ([touch.view.superview isKindOfClass:[UITableViewCell class]]) {
        return NO;
    }
    return YES;
}

@end
